/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import connection.ConnectionDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.CarBooking;

/**
 *
 * @author Nhang Con
 */
public class CarBookingDAL {

    private ConnectionDB conn = new ConnectionDB();
    private CallableStatement cs;
    private ResultSet rs;

    public List<CarBooking> getListCarBooking() {
        List<CarBooking> listCarBooking = new ArrayList<CarBooking>();
        CarBooking carbooking = null;
        try {
            cs = conn.createConnection().prepareCall("{call sp_ListBookingCar()}");
            rs = cs.executeQuery();
            while (rs.next()) {
                carbooking = new CarBooking();
                carbooking.setCustomerID(rs.getInt("CustomerID"));
                carbooking.setCarBookingID(rs.getInt("CarBookingID"));
                carbooking.setQuantity(rs.getInt("Quantity"));
                carbooking.setCarID(rs.getInt("CarID"));
                carbooking.setPickUpDate(rs.getDate("PickupDate"));
                carbooking.setDropOffDate(rs.getDate("DropoffDate"));
                carbooking.setPrice(rs.getFloat("TotalPrice"));
                carbooking.setIsDriver(rs.getBoolean("Driver"));
                carbooking.setAvaiable(rs.getBoolean("Available"));
                listCarBooking.add(carbooking);
            }
            return listCarBooking;
        } catch (SQLException ex) {
            Logger.getLogger(CarBookingDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    public String getCarName(int id) {
        String name = null;
        try {

            cs = conn.createConnection().prepareCall("{call sp_getCarName(?)}");
            cs.setInt("intcar", id);
            rs = cs.executeQuery();
            while (rs.next()) {
                name = rs.getString("CarName");

            }
            return name;
        } catch (SQLException ex) {
            Logger.getLogger(CarBookingDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    public boolean updateStatusCarBooking(int idbooking) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_updateStatusCarBooking(?)}");
            cs.setInt("idBooking", idbooking);
            if (cs.executeUpdate() == 1) {
                return true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(CarBookingDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;

        }
        return false;


    }
}
